from flask import request, url_for, render_template, redirect, Blueprint, current_app
from datetime import datetime
from drifting_bottle_bot import data_to_bot
from drifting_bottle_bot_sql import select_table_opt
from my_util import Logger, decode_base64

# initialize our Flask application and the lpr object
blueprint = Blueprint('bot_process', __name__)

lprObject = None
loger = Logger()
default_pic_name = "defaultPlate.jpg"

MAX_SHOW_LEN = 50    # 历史页面展示的列最大长度

@blueprint.route('/procs', methods=['GET', 'POST'])  # 访问的路径
def bot_submit():
    status = '未提交'
    if request.method == 'POST':
        # 获取textarea提交的文本
        question = request.form.get("questionTxt")
        if question:
            loger.info("get question " + question)
            data_to_bot(question)
            status = '已提交'
        else:
            loger.warning("none")

    return render_template('bot_question_page.html', status=status)


@blueprint.route('/history', methods=['GET', 'POST'])  # 访问的路径
def bot_history():
    result = select_table_opt(datetime.now(), 9999)
    data = []
    for row in result:
        if row[2] is not None and row[5] is not None:
            res_exchange = {'id': int(row[0]), 'atime': row[1], 'qtime': row[2],
                            'question': decode_base64(row[3])[:MAX_SHOW_LEN], 'answer': decode_base64(row[5])[:2*MAX_SHOW_LEN]}
        elif row[2] is not None:
            res_exchange = {'id': int(row[0]), 'atime': row[1], 'qtime': row[2],
                            'question': decode_base64(row[3])[:MAX_SHOW_LEN], 'answer': '未处理'}
        else:
            res_exchange = {'id': int(row[0]), 'atime': row[1], 'qtime': '未处理',
                            'question': decode_base64(row[3])[:MAX_SHOW_LEN], 'answer': '未处理'}
        loger.debug(res_exchange)
        data.append(res_exchange)

    return render_template('bot_history_page.html', data=data)


@blueprint.route('/botQA', methods=['GET', 'POST'])  # 访问的路径
def bot_QA():
    id = request.args.get('id')
    loger.debug(f"get id is {id}")
    data1 = []
    data = select_table_opt(datetime.now(), 9999)
    for res in data:
        if res[0] == int(id):
            data1 = res
            loger.debug(f"bot Q/A :{data1}")
            break

    if data1[2] is not None and data1[5] is not None:
        res_exchange = {'id': int(data1[0]), 'atime': data1[1], 'qtime': data1[2],
                        'question': decode_base64(data1[3]), 'answer': decode_base64(data1[5])}
    elif data1[2] is not None:
        res_exchange = {'id': int(data1[0]), 'atime': data1[1], 'qtime': data1[2],
                        'question': decode_base64(data1[3]), 'answer': '未处理'}
    else:
        res_exchange = {'id': int(data1[0]), 'atime': data1[1], 'qtime': '未处理',
                        'question': decode_base64(data1[3]), 'answer': '未处理'}

    loger.info(f"res_exchange is {res_exchange}")
    return render_template('bot_QA_page.html', data=res_exchange)